package cn.com.zhbs.login.controller;

import cn.com.zhbs.account.entitys.Account;
import cn.com.zhbs.common.Result.SimpleFactory.ResultFactory;
import cn.com.zhbs.common.ucpaas.Testmsm;
import cn.com.zhbs.login.dao.LoginMapper;
import cn.com.zhbs.login.service.ForgetPasswordService;
import cn.com.zhbs.resource.MSGConfig;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * @program: vr_manage
 * @description: 忘记密码
 * @author: 陈治杰
 * @create: 2021-08-09 10:20
 **/
@Controller
@RequestMapping("forgetPassword")
public class forgetPasswordController {
    @Resource
    LoginMapper loginMapper;
    @Resource
    ForgetPasswordService forgetPasswordService;

    /**
     　　* @Description: 向手机发送短信验证码
     　　* @param ACCOUNT
     　　* @return ${return_type}
     　　* @throws
     　　* @author 陈治杰
     　　* @date 2020/7/14 17:40
     　　*/
    @RequestMapping(value = "sendCode", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> sendCode(@RequestBody JSONObject jsonObject, HttpServletRequest request){
        Map<String,Object> requestMap=new HashMap<>();
        HttpSession session=request.getSession();
        try{
            requestMap.put(Account.mobilephone.toString(),jsonObject.get(Account.mobilephone.toString()));
            String account=loginMapper.isPhoneExist(requestMap);
            if(account==null){
                return ResultFactory.failure("0","该账户不存在",null).display();
            }else{
                String var = new String();
                for (int j = 0; j < 6; j++) {
                    Random r = new Random();
                    if (r.nextInt(2) < 1) {
                        String chars = "ABCDEFGHIJKLMNPQRSTUVWXYZ";
                        var = var + chars.charAt((int) (Math.random() * 25));
                    } else {
                        String chars = "123456789";
                        var = var + chars.charAt((int) (Math.random() * 9));
                    }

                }
                boolean json=true;
                System.out.println(var);
                String templateId="28815";
                String para=var+",3";
                System.out.println(Testmsm.testTemplateSMS(json, MSGConfig.ACCOUNT_SID,MSGConfig.TOKEN,MSGConfig.APPID, templateId,requestMap.get(Account.mobilephone.toString()).toString(),para));
                session.setAttribute(Account.mobilephone.toString(),requestMap.get(Account.mobilephone.toString()));
                session.setAttribute(Account.account.toString(),account);
                session.setAttribute("code",var);
                session.setAttribute("due",System.currentTimeMillis());
                return ResultFactory.success().display();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultFactory.failure().display();
    }

    /**
     　　* @Description: 判断验证码是否正确
     　　* @param code 验证码
     　　* @return ${return_type}
     　　* @throws
     　　* @author 陈治杰
     　　* @date 2020/7/14 17:40
     　　*/
    @RequestMapping(value = "vertifyCode", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> vertifyCode(@RequestBody JSONObject jsonObject,HttpServletRequest request){
        HttpSession session=request.getSession();
        try{
            if(System.currentTimeMillis()-(Long)session.getAttribute("due")<180*1000){
                if(session.getAttribute("code").equals(jsonObject.get("code").toString().toUpperCase())){
                    return ResultFactory.success().display();
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultFactory.failure().display();
    }

    /**
     　　* @Description: 修改密码
     　　* @param code 验证码
     　　* @return ${return_type}
     　　* @throws
     　　* @author 陈治杰
     　　* @date 2020/7/14 17:40
     　　*/
    @RequestMapping(value = "changePassword",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> changePassword(@RequestBody JSONObject jsonObject, HttpServletRequest request, HttpServletResponse response){
        HttpSession session=request.getSession();
        try{
            jsonObject.put(Account.account.toString(),session.getAttribute(Account.account.toString()));
            forgetPasswordService.editPassword(jsonObject);
            return ResultFactory.success().display();
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultFactory.failure().display();
    }
}
